$(function () {

    var currentLanguage=$(".headList").attr("data");
    var currentOrder=$(".order").attr("data");
    var currentSize=$(".size").attr("data");
    var currentTime=$(".time").attr("data");
    var headList=$(".headList ul li");

    for(var i=0;i<headList.length;i++){
        if($(".headList").find("a").eq(i).text()===currentLanguage){
            $(".headList").find("li").eq(i).css("border-bottom","2px solid #FF5722");
            break;
        }
    }

    switch(currentOrder){
        case "综合排序": $(".order").find("a").eq(0).css({"background-color":"dodgerblue","border-radius":"10%","color":"white"});break;
        case "点击最多": $(".order").find("a").eq(1).css({"background-color":"dodgerblue","border-radius":"10%","color":"white"});break;
        case "点赞最多": $(".order").find("a").eq(2).css({"background-color":"dodgerblue","border-radius":"10%","color":"white"});break;
        case "收藏最多": $(".order").find("a").eq(3).css({"background-color":"dodgerblue","border-radius":"10%","color":"white"});break;
        case "下载最多": $(".order").find("a").eq(4).css({"background-color":"dodgerblue","border-radius":"10%","color":"white"});break;
    }
    switch (currentSize) {
        case "全部大小": $(".size").find("a").eq(0).css({"background-color":"dodgerblue","border-radius":"10%","color":"white"});break;
        case "小于1M": $(".size").find("a").eq(1).css({"background-color":"dodgerblue","border-radius":"10%","color":"white"});break;
        case "1~10M": $(".size").find("a").eq(2).css({"background-color":"dodgerblue","border-radius":"10%","color":"white"});break;
        case "10~30M": $(".size").find("a").eq(3).css({"background-color":"dodgerblue","border-radius":"10%","color":"white"});break;
        case "30~100M": $(".size").find("a").eq(4).css({"background-color":"dodgerblue","border-radius":"10%","color":"white"});break;
        case "100M以上": $(".size").find("a").eq(5).css({"background-color":"dodgerblue","border-radius":"10%","color":"white"});break;
    }
    switch(currentTime){
        case "全部时间": $(".time").find("a").eq(0).css({"background-color":"dodgerblue","border-radius":"10%","color":"white"});break;
        case "2021": $(".time").find("a").eq(1).css({"background-color":"dodgerblue","border-radius":"10%","color":"white"});break;
        case "2020": $(".time").find("a").eq(2).css({"background-color":"dodgerblue","border-radius":"10%","color":"white"});break;
        case "2019": $(".time").find("a").eq(3).css({"background-color":"dodgerblue","border-radius":"10%","color":"white"});break;
        case "2018": $(".time").find("a").eq(4).css({"background-color":"dodgerblue","border-radius":"10%","color":"white"});break;
        case "更早": $(".time").find("a").eq(5).css({"background-color":"dodgerblue","border-radius":"10%","color":"white"});break;
    }

    $(".headList li").on("click",function () {
        $(".headList li").css("border-bottom","");
        $(this).css("border-bottom","2px solid #FF5722");
    });
    $(".filter ul li a").on("click",function () {
        $(this).parent().parent().children().children().css({"background-color":"","border-radius":"","color":""});
        $(this).css({"background-color":"dodgerblue","border-radius":"10%","color":"white"});
    });

    //下载事件
    $(".download").on("click",function () {
        var objectid=$(this).attr("data");//获取当前点击书籍的objectid
        // console.log(objectid);
        // console.log($(".downloadCount-" + objectid).text());
        var downloadCounts = $(".downloadCount-" + objectid);//获取当前书籍的下载次数
        var events={"eventType":"下载","value":objectid};
        $.ajax({
            url: "bookDetail/userEvent",
            method: "POST",
            // dataType: "json",  若后端传来的不是json数据，success将不执行或者读不到
            contentType: "application/json;charset=UTF-8",
            data: JSON.stringify(events),
            success: function (result) {
                console.log(result);
                if(result==="download successful"){
                    var newValues=Number(downloadCounts.text())+1;
                    downloadCounts.text(newValues);
                    alert("下载成功!");
                }else{
                    alert("下载失败！请重试");
                }
            }
        })
    });
});